package com.zc.controller;

import com.zc.entity.Item;
import com.zc.entity.Userandteam;
import com.zc.entity.Userteam;
import com.zc.mapper.ItemMapper;
import com.zc.mapper.UserandteamMapper;
import com.zc.mapper.UserteamMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
public class Item2Controller {
    Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    UserteamMapper userteamMapper;
    @Autowired
    ItemMapper itemMapper;
    @Autowired
    UserandteamMapper userandteamMapper;
    @GetMapping("/item")
    public  String  getItem(HttpSession session, Model model)
    {
        int id =(int)session.getAttribute("userid");
        Userteam userteam =userteamMapper.selectByUserId(id);
        if(userteam==null)
        {

            return "team/teamadd";
        }else {
            Item item = itemMapper.selectByTeamId(userteam.getId());
            if(item==null)
            {
                return "item/itemadd";
            }else {
                model.addAttribute("item",item);
                return "item/item";
            }
        }

    }
    @PostMapping("/item")
    public String itemAdd(Item item,HttpSession session,Model model)
    {
        item.setNewdate(new Date());
        item.setStatus("0-正常");
        Userandteam userandteam = userandteamMapper.selectByPrimaryKey((int) session.getAttribute("userid"));
        item.setTeamid(userandteam.getTeamid());
        itemMapper.insert(item);
        model.addAttribute("item",item);
        return "item/item";
    }
}
